package com.eastfair.imaster.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.annotation.base.LanguageTypeEnum;
import com.eastfair.annotation.base.SubjectTypeEnum;
import com.eastfair.config.api.SwitchServiceFeign;
import com.eastfair.config.entity.Switch;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.imaster.entity.VisitorRegister;
import com.eastfair.imaster.service.ConfigurationInfoService;
import com.eastfair.imaster.service.VisitorRegisterService;
import com.eastfair.imaster.vo.ConfigurationInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Date:2021/12/7
 * Time:10:22
 *
 * @author zjn
 */
@Service
@DS("#thread.tenant")
public class ConfigurationInfoServiceImpl implements ConfigurationInfoService {

    @Autowired
    private SwitchServiceFeign switchServiceFeign;
    @Autowired
    private VisitorRegisterService visitorRegisterService;

    @Override
    public R<ConfigurationInfoVO> getConfigurationInfo() {
        ConfigurationInfoVO configurationInfoVO = new ConfigurationInfoVO();

        // 获取用户语言
        LanguageTypeEnum userLanguage = getUserLanguage();

        Switch sw = Switch.builder().subjectType(ContextUtil.getSubjectType()).languageType(userLanguage.name()).build();
        List<Switch> switchList = switchServiceFeign.getList(sw).getData();
        if (CollectionUtil.isNotEmpty(switchList)) {
            Map<String, Integer> configSwitch = switchList.stream().collect(Collectors.toMap(Switch::getKeyword, Switch::getStatus));
            configurationInfoVO.setConfigSwitch(configSwitch);
        }


        return R.success(configurationInfoVO);
    }

    /**
     * 获取当前用户语言
     *
     * @return 当前用户语言
     */
    public LanguageTypeEnum getUserLanguage() {
        LanguageTypeEnum userLanguage = LanguageTypeEnum.parse(ContextUtil.getLanguageType());
        if (SubjectTypeEnum.VISITOR.name().equals(ContextUtil.getSubjectType())) {
            QueryWrapper<VisitorRegister> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(VisitorRegister::getProjectId, ContextUtil.getProjectId())
                    .eq(VisitorRegister::getExhibitionId, ContextUtil.getExhibitionId())
                    .eq(VisitorRegister::getIsEnabled, BusinessConstant.ENABLE_YES)
                    .eq(VisitorRegister::getIsDeleted, BusinessConstant.DELETE_NO)
                    .eq(VisitorRegister::getUserAccountId, ContextUtil.getUserId());
            VisitorRegister visitorRegister = visitorRegisterService.getOne(queryWrapper, true);
            if (!Objects.isNull(visitorRegister)) {
                return LanguageTypeEnum.parseByCountry(visitorRegister.getCountryId());
            }
        } else if (SubjectTypeEnum.ACTOR.name().equals(ContextUtil.getSubjectType())) {

        }
        return userLanguage;
    }
}
